//=============================================================================

#include "../../../../inc/aSkin.h"

//=============================================================================

aSkin::aSkin()
{
}

//=============================================================================

bool aSkin::asIsExist() const
{
  if (texs.alGetCount() > 0)
    return 1;

  return 0;
}

//=============================================================================

void aSkin::asBindSkin() const
{
  texs.alGetLastItem()->data.atBindTexture();
}

//=============================================================================

aItem<aTexture>* aSkin::asAddTexture(const aString& path)
{
  if (path.asGetLength() <= 0)
    return 0;
  
  aItem<aTexture>* tmpTexture = texs.alAddItem();

  if (tmpTexture->data.atLoadTextureTGA(path))
    return tmpTexture;

  return 0;
}

//=============================================================================

void aSkin::asRemoveSkinData()
{
  texs.alRemoveItems();
}

//=============================================================================

size_t aSkin::asGetDataVolume() const
{
  size_t res = 0;
  int count = texs.alGetCount();
  aItem<aTexture>* tmpTexture = texs.alGetFirstItem();

  res += sizeof(unsigned short)*count*2;//width & height value + channels
  while(tmpTexture)
  {
    res += tmpTexture->data.atGetWidth()*tmpTexture->data.atGetHeight()*tmpTexture->data.atGetChannels();//main data
    tmpTexture = tmpTexture->next;
  }

  return res;
}

//=============================================================================

askRet aSkin::asCollectData(void* data, size_t wholeSize) const
{
  if (wholeSize <= 0)
    return SKIN_COL_BAD_DATA_SIZE;

  if (!data)
    return SKIN_COL_NULL_DATA_POINTER;

  int tCount = texs.alGetCount();

  if (tCount <= 0)
    return SKIN_COL_NO_TEXTURES;

  aItem<aTexture>* tmpTexture = texs.alGetFirstItem();
  unsigned short tmpValue;
  size_t shortSize = sizeof(unsigned short);
  size_t shift = 0;
  char* tmpPointer = (char*)data;

  while(tmpTexture)
  {
    tmpValue = tmpTexture->data.atGetWidth();

    memcpy(&tmpPointer[shift], &tmpValue, shortSize);
    shift += shortSize;

    tmpValue = tmpTexture->data.atGetChannels();

    memcpy(&tmpPointer[shift], &tmpValue, shortSize);
    shift += shortSize;

    if (!tmpTexture->data.atCollectDataTo(&tmpPointer[shift]))
      return SKIN_COL_UNSUSPECT_ERROR;

    shift += tmpTexture->data.atGetWidth()*tmpTexture->data.atGetHeight()*tmpTexture->data.atGetChannels();

    tmpTexture = tmpTexture->next;
  }

  return SKIN_DONE;
}

//=============================================================================

askRet aSkin::asAbsorbData(char* data, int tCount)
{
    if (!data)
        return SKIN_ABS_BAD_SKIN_DATA_POINTER;

    if (tCount <= 0)
        return SKIN_ABS_BAD_TEXTURES_COUNT;

    int i = 0;
    aItem<aTexture>* tmpTexture = 0;
    unsigned short tmpValue[2];
    size_t shift = 0;

    for (;i < tCount; ++i)
    {
        tmpTexture = texs.alAddItem();
        memcpy(&tmpValue[0], data, sizeof(unsigned short)*2);
        shift += sizeof(unsigned short)*2;
        if (tmpTexture->data.atAbsorbData(&data[shift], tmpValue[0], tmpValue[1]))
        {
          tmpTexture->data.atGenerateTextureForOGL();
          shift += tmpValue[0]*tmpValue[0]*tmpValue[1];
        }

    }

  return SKIN_DONE;
}

//=============================================================================

void aSkin::asCloneSkin(const aSkin &skin)
{
  asRemoveSkinData();

  aItem<aTexture>* tmpTexture = skin.texs.alGetFirstItem();

  while(tmpTexture)
  {
    texs.alAddItem()->data.atCloneTexture(tmpTexture->data);

    tmpTexture = tmpTexture->next;
  }
}

//=============================================================================

aSkin::~aSkin()
{
}

//=============================================================================
